package com.example.sqlitedemo.activity;

import android.content.ContentValues;
import android.database.sqlite.SQLiteDatabase;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;

import com.example.sqlitedemo.R;
import com.example.sqlitedemo.sqlite.Constant;
import com.example.sqlitedemo.sqlite.DBManage;
import com.example.sqlitedemo.sqlite.MySQLiteHelper;

public class MainActivity extends AppCompatActivity implements View.OnClickListener {
    private Button btn_newDB,btn_insert,btn_update,btn_delete;
    private Button btn_insert_api,btn_update_api,btn_delete_api;
    private MySQLiteHelper helper;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        btn_newDB = findViewById(R.id.btn_newDB);
        btn_insert = findViewById(R.id.btn_insert);
        btn_update = findViewById(R.id.btn_update);
        btn_delete = findViewById(R.id.btn_delete);
        btn_insert_api = findViewById(R.id.btn_insert_api);
        btn_update_api = findViewById(R.id.btn_update_api);
        btn_delete_api = findViewById(R.id.btn_delete_api);
        helper = DBManage.getHelper(MainActivity.this);
        btn_newDB.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                SQLiteDatabase db = helper.getWritableDatabase();
            }
        });

        btn_insert.setOnClickListener(this);
        btn_update.setOnClickListener(this);
        btn_delete.setOnClickListener(this);
        btn_delete_api.setOnClickListener(this);
        btn_update_api.setOnClickListener(this);
        btn_insert_api.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        switch(v.getId()){
            case R.id.btn_insert:
                SQLiteDatabase db = helper.getWritableDatabase();
                String sql = "insert into "+Constant.TABLE_NAME +" values(1,'name1')";
                DBManage.execSQL(db,sql);

                String sql2 = "insert into "+Constant.TABLE_NAME +" values(2,'name2')";
                DBManage.execSQL(db,sql2);

                String sql3 = "insert into "+Constant.TABLE_NAME +" values(3,'name3')";
                DBManage.execSQL(db,sql3);
                db.close();
                break;
            case R.id.btn_update:
                db = helper.getWritableDatabase();
                String updateSql = "update "+Constant.TABLE_NAME+" set "+Constant.NAME+" = 'zhangsan' where "+Constant.SID+" = 2";
                DBManage.execSQL(db,updateSql);
                db.close();
                break;
            case R.id.btn_delete:
                db = helper.getWritableDatabase();
                String deleteSql = "delete from "+Constant.TABLE_NAME+" where "+Constant.SID+" = 1";
                DBManage.execSQL(db,deleteSql);
                db.close();
                break;
            case R.id.btn_insert_api:
                db = helper.getWritableDatabase();
                ContentValues contentValues = new ContentValues();
                //contentValues.put(Constant.SID,1);
                contentValues.put(Constant.NAME,"adadafsdfs");
                db.insert(Constant.TABLE_NAME,null,contentValues);
                db.close();
                break;
            case R.id.btn_update_api:
                ContentValues updateData = new ContentValues();
                updateData.put(Constant.NAME,"wangwu");
                db = helper.getWritableDatabase();
                db.update(Constant.TABLE_NAME,updateData,Constant.SID+"=1",null);
                // ? ---> 占位符
                db.update(Constant.TABLE_NAME,updateData,Constant.SID+"=?",new String[]{"3"});
                db.close();
                break;
            case R.id.btn_delete_api:
                db = helper.getWritableDatabase();
//                db.delete(Constant.TABLE_NAME,Constant.SID+"=1",null);
                // 效果同上
                db.delete(Constant.TABLE_NAME,Constant.SID+"=?",new String[]{"1"});
                db.close();
                break;

        }
    }
}
